<?php

namespace App\Http\Controllers\Api\Wechat;

use App\Api\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Models\Dream\Activity\Sphere\Content;
use App\Models\User;
use Illuminate\Http\Request;
use GuzzleHttp\Client;
use App\Http\Controllers\Api\Wechat\JsSdkController;
use Illuminate\Support\Facades\Auth;

class MessagesController extends Controller
{
    use ApiResponse;
    /**
     * 微信订阅消息
     */

    //设置订阅消息参数
    public function setSubMsg($template_id,$openid,$user,$name ='',$data,JsSdkController $jsSdkController)
    {
        //获取微信 access_token
       $access_token = $jsSdkController->getAccessToken();

        //设置接口参数
        $datas = [
            'template_id' => 'XXX',//所需下发的订阅模板id
            'touser' => 'XXXX',//接收者（用户）的 openid
            'page' => '',//不跳转
            'data' => [
                'thing1' => [
                    'value' => '活动名',
                ],
                'name1' => [
                    'value' => '用户名昵称',
                ],
//                'phone\_number4' => [
//                    'value' => '电话',
//                ],
//                'phrase14' => [
//                    'value' => 'xx',
//                ],
                'date' => [
                    'value' => '2020年01月05日',
                ],
            ],
        ];

        $client = new Client('https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$access_token);
        $itemsResponse = $client->request('post','',
            ['json' => $datas,
                'headers' => [
                    'Content-type'=> 'application/json',
//                'Cookie'=> 'XDEBUG_SESSION=PHPSTORM',
                    "Accept"=>"application/json"]
            ]);

        $data = $itemsResponse->getBody()->getContents();

        return $data;
    }

    //获取订阅信息
    public function getSubMsg(Request $request)
    {
        $user = Auth::guard('api')->user();
        $weixin_openid = User::where('id',$user->id)->value('weixin_openid');

        $template_id = $request->template_id;//所需下发的订阅模板id

        $datas = Content::where('status',1)->first();


        return $datas;
    }



}
